﻿#include "QtVtkModleWindow.h"
#include "QtVtkModleWindow.h"
#include "SARibbonApplicationButton.h"
#include "SARibbonBar.h"
#include "SARibbonButtonGroupWidget.h"
#include "SARibbonCategory.h"
#include "SARibbonCheckBox.h"
#include "SARibbonColorToolButton.h"
#include "SARibbonComboBox.h"
#include "SARibbonCustomizeDialog.h"
#include "SARibbonCustomizeWidget.h"
#include "SARibbonGallery.h"
#include "SARibbonLineEdit.h"
#include "SARibbonMenu.h"
#include "SARibbonPannel.h"
#include "SARibbonQuickAccessBar.h"
#include "SARibbonToolButton.h"
#include "SARibbonSystemButtonBar.h"
#include <QAbstractButton>
#include <QAction>
#include <QApplication>
#include <QButtonGroup>
#include <QCalendarWidget>
#include <QDebug>
#include <QElapsedTimer>
#include <QFile>
#include <QFileDialog>
#include <QFontComboBox>
#include <QLabel>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
#include <QStatusBar>
#include <QTextEdit>
#include <QTextStream>
#include <QXmlStreamWriter>
#include <QMessageBox>
#include <QShortcut>
#include <QLineEdit>
#include <QDialogButtonBox>
#include <QSurfaceFormat>
#include <QVTKOpenGLNativeWidget.h>
#include <QSplitter.h>

#include "MyVtkViewWindow.h"
#include "CreatePointDlg.h"
#include "CreateLineDlg.h"
#include "CreatePlaneDlg.h"
#include "CreateCubeDlg.h"
#include "CreateConeDlg.h"
#include "CreateCylinderDlg.h"
#include "CreateSphereDlg.h"
#include "CreateRectangleDlg.h"
#include "CreateTorusDlg.h"
#include "CreateVolumeDlg.h"
#include "CreateCircleDlg.h"




QtVtkModleWindow::QtVtkModleWindow(QWidget* parent)
    : SARibbonMainWindow(parent)
{
    InitUi();
}

QtVtkModleWindow::~QtVtkModleWindow()
{
}

void QtVtkModleWindow::InitUi()
{
    //设置窗口大小和位置
    resize(1000, 800); // 调整窗口大小
    move(300, 120);// 移动窗口位置


    setWindowTitle(("QT VTK Modle"));
    setWindowIcon(QIcon("../icon/customize.png"));   //显示应用图标
    setStatusBar(new QStatusBar());

    //创建菜单栏
    {
        SARibbonBar* ribbon = ribbonBar();
        ribbon->setContentsMargins(5, 5, 5, 5);  //! 通过setContentsMargins设置ribbon四周的间距
        //ribbon->setRibbonStyle(SARibbonBar::RibbonStyleThreeRow);//设置  SARibbonBar::RibbonStyleCompactTwoRow

        CreateRibbonApplicationButton();  //显示应用程序按钮
        CreateQuickAccessBar();//创建快速访问栏
        CreateRightButtonGroup();//创建右侧按钮组示例

        SARibbonCategory* pCategoryModel = ribbon->addCategoryPage(tr("&Model"));//创建主页
        pCategoryModel->setObjectName(("CategoryModel"));
        CreateCategoryMain(pCategoryModel);

    }

    //创建中心区域
    {
        auto centerWidget = new QWidget(this);
        centerWidget->setObjectName(("centerWidget"));
        centerWidget->setContentsMargins(5, 5, 5, 5);
        centerWidget->setStyleSheet("background-color:white;");

        QVBoxLayout* pCenterLayout = new QVBoxLayout(centerWidget);
        pCenterLayout->setContentsMargins(0, 0, 0, 0);
        pCenterLayout->setSpacing(0);

        {
            //创建qt splitter
            auto CenterSplitter = new QSplitter(centerWidget);
            CenterSplitter->setOrientation(Qt::Horizontal);
            CenterSplitter->setHandleWidth(10);
            CenterSplitter->setContentsMargins(5, 5, 5, 5);
            pCenterLayout->addWidget(CenterSplitter);

            //设置qtsplitter 设置比例
            CenterSplitter->setStretchFactor(0, 2);
            CenterSplitter->setStretchFactor(1, 6);
            CenterSplitter->setStretchFactor(2, 2);


            //创建qt leftTabWidget    
            {
                auto lefTtabWidget = new QTabWidget(CenterSplitter);
                lefTtabWidget->setContentsMargins(0, 0, 0, 0);
                lefTtabWidget->setTabPosition(QTabWidget::South);
                lefTtabWidget->setDocumentMode(true);
                lefTtabWidget->setTabsClosable(false);
                lefTtabWidget->setMovable(false);
                lefTtabWidget->setTabBarAutoHide(false);
                lefTtabWidget->setTabShape(QTabWidget::Triangular);
                lefTtabWidget->setElideMode(Qt::ElideRight);
                lefTtabWidget->setUsesScrollButtons(true);
                lefTtabWidget->setMinimumWidth(200);
                CenterSplitter->addWidget(lefTtabWidget);

                //添加模型树组件
                m_modelTreeWidget = CreateModelDataWidget(lefTtabWidget);
                lefTtabWidget->addTab(m_modelTreeWidget, tr("Model Tree"));
            }

            {
                //创建qt splitter
                auto viewArearSplitter = new QSplitter(centerWidget);
                viewArearSplitter->setOrientation(Qt::Vertical);
                viewArearSplitter->setHandleWidth(10);
                viewArearSplitter->setContentsMargins(5, 5, 5, 5);
                CenterSplitter->addWidget(viewArearSplitter);

                //设置qtsplitter 设置比例
                viewArearSplitter->setStretchFactor(0, 4);
                viewArearSplitter->setStretchFactor(1, 4);

                {
                    auto viewWidget = CreateCenterViewWidget(viewArearSplitter);
                    viewArearSplitter->addWidget(viewWidget);
                }

                //创建qt buttomTtabWidget
                {
                    auto buttomTtabWidget = new QTabWidget(viewArearSplitter);
                    buttomTtabWidget->setContentsMargins(0, 0, 0, 0);
                    buttomTtabWidget->setTabPosition(QTabWidget::South);
                    buttomTtabWidget->setDocumentMode(true);
                    buttomTtabWidget->setTabsClosable(false);
                    buttomTtabWidget->setMovable(false);
                    buttomTtabWidget->setTabBarAutoHide(false);
                    buttomTtabWidget->setTabShape(QTabWidget::Triangular);
                    buttomTtabWidget->setElideMode(Qt::ElideRight);
                    buttomTtabWidget->setUsesScrollButtons(true);
                    buttomTtabWidget->setMaximumHeight(400);
                    buttomTtabWidget->setMinimumHeight(200);
                    viewArearSplitter->addWidget(buttomTtabWidget);
                }
            }

            //创建qt rightTabWidget
            {
                auto rightTabWidget = new QTabWidget(CenterSplitter);
                rightTabWidget->setContentsMargins(0, 0, 0, 0);
                rightTabWidget->setTabPosition(QTabWidget::South);
                rightTabWidget->setDocumentMode(true);
                rightTabWidget->setTabsClosable(false);
                rightTabWidget->setMovable(false);
                rightTabWidget->setTabBarAutoHide(false);
                rightTabWidget->setTabShape(QTabWidget::Triangular);
                rightTabWidget->setElideMode(Qt::ElideRight);
                rightTabWidget->setUsesScrollButtons(true);
                rightTabWidget->setMinimumWidth(200);
                CenterSplitter->addWidget(rightTabWidget);
            }
        }

        centerWidget->setLayout(pCenterLayout);
        setCentralWidget(centerWidget);
    }
}

void QtVtkModleWindow::CreateRibbonApplicationButton()
{
    SARibbonBar* ribbon = ribbonBar();
    if(!ribbon)
    {
        return;
    }

    if(!m_pMenuApplicationBtn)
    {
        m_pMenuApplicationBtn = new SARibbonMenu(this);
        m_pMenuApplicationBtn->addAction(CreateAction("test1", "../icon/CreateIcon/My_Create_Point.png"));
        m_pMenuApplicationBtn->addAction(CreateAction("test2", "../icon/action2.svg"));
        m_pMenuApplicationBtn->addAction(CreateAction("test3", "../icon/action3.svg"));
        m_pMenuApplicationBtn->addAction(CreateAction("test4", "../icon/action4.svg"));

        m_pMenuApplicationBtn->setWindowIcon(QIcon("../icon/customize.png"));
        m_pMenuApplicationBtn->setWindowTitle(tr("Application"));
        m_pMenuApplicationBtn->setFixedWidth(200);
        m_pMenuApplicationBtn->setFixedHeight(200);
        m_pMenuApplicationBtn->setContentsMargins(5, 5, 5, 5);
        m_pMenuApplicationBtn->setStyleSheet("background-color:white;");
    }


    SARibbonApplicationButton* pBtn = new SARibbonApplicationButton(this);
    pBtn->setText(("  &File  "));  // 文字两边留有间距，好看一点
    pBtn->setMenu(m_pMenuApplicationBtn);
    ribbon->setApplicationButton(pBtn);

}

void QtVtkModleWindow::CreateCategoryMain(SARibbonCategory* page)
{
    CreateCategoryCreate(page);
    CreateCategoryView(page);
}

void QtVtkModleWindow::CreateCategoryCreate(SARibbonCategory* page)
{
    // 使用addPannel函数来创建SARibbonPannel，效果和new SARibbonPannel再addPannel一样
    SARibbonPannel* pPannelCreateGeometry = page->addPannel(tr("Create"));
    pPannelCreateGeometry->setToolTip(tr("Create geometry"));
    pPannelCreateGeometry->setObjectName(("pannelCreateGeometry"));

    {
        QAction* pActionCreatePoint = CreateAction(tr("Point"), "../icon/CreateIcon/My_Create_Point.png");
        pPannelCreateGeometry->addLargeAction(pActionCreatePoint);
        connect(pActionCreatePoint, &QAction::triggered, this, [=]()
                {
                    CreatePointDlg createPointDlg;
                    createPointDlg.exec();
                });
    }

    {
        QAction* pActionCreateLine = CreateAction(tr("Line"), "../icon/CreateIcon/My_Create_Line.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateLine);
        connect(pActionCreateLine, &QAction::triggered, this, [=]()
                {
                    CreateLineDlg createLineDlg;
                    createLineDlg.exec();
                });
    }

    {
        QAction* pActionCreateRectangle = CreateAction(tr("Rectangle"), "../icon/CreateIcon/My_Create_Rectangle.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateRectangle);
        connect(pActionCreateRectangle, &QAction::triggered, this, [=]()
                {
                    CreateRectangleDlg createRectangleDlg;
                    createRectangleDlg.exec();
                });
    }

    {
        QAction* pActionCreateCircle = CreateAction(tr("Circle"), "../icon/CreateIcon/My_Create_Circle.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateCircle);
        connect(pActionCreateCircle, &QAction::triggered, this, [=]()
                {
                    CreateCircleDlg createCircleDlg;
                    createCircleDlg.exec();
                });
    }

    {
        QAction* pActionCreatePlane = CreateAction(tr("Plane"), "../icon/CreateIcon/My_Create_Surface.png");
        pPannelCreateGeometry->addLargeAction(pActionCreatePlane);
        connect(pActionCreatePlane, &QAction::triggered, this, [=]()
                {
                    CreatePlaneDlg createSurfaceDlg;
                    createSurfaceDlg.exec();
                });
    }

    {
        QAction* pActionCreateSphere = CreateAction(tr("Sphere"), "../icon/CreateIcon/My_Create_Sphere.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateSphere);
        connect(pActionCreateSphere, &QAction::triggered, this, [=]()
                {
                    CreateSphereDlg createSphereDlg;
                    createSphereDlg.exec();
                });
    }

    {
        QAction* pActionCreateCone = CreateAction(tr("Cone"), "../icon/CreateIcon/My_Create_Cone.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateCone);
        connect(pActionCreateCone, &QAction::triggered, this, [=]()
                {
                    CreateConeDlg createConeDlg;
                    createConeDlg.exec();
                });
    }

    {
        QAction* pActionCreateCube = CreateAction(tr("Cube"), "../icon/CreateIcon/My_Create_Cube.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateCube);
        connect(pActionCreateCube, &QAction::triggered, this, [=]()
                {
                    CreateCubeDlg createCubeDlg;
                    createCubeDlg.exec();
                });
    }

    {
        QAction* pActionCreateCylinder = CreateAction(tr("Cylinder"), "../icon/CreateIcon/My_Create_Cylinder.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateCylinder);
        connect(pActionCreateCylinder, &QAction::triggered, this, [=]()
                {
                    CreateCylinderDlg createCylinderDlg;
                    createCylinderDlg.exec();
                });
    }

    {
        QAction* pActionCreateTorus = CreateAction(tr("Torus"), "../icon/CreateIcon/My_Create_Torus.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateTorus);
        connect(pActionCreateTorus, &QAction::triggered, this, [=]()
                {
                    CreateTorusDlg createTorusDlg;
                    createTorusDlg.exec();
                });
    }

    {
        QAction* pActionCreateVolume = CreateAction(tr("Volume"), "../icon/CreateIcon/My_Create_Volume.png");
        pPannelCreateGeometry->addLargeAction(pActionCreateVolume);
        connect(pActionCreateVolume, &QAction::triggered, this, [=]()
                {
                    CreateVolumeDlg createVolumeDlg;
                    createVolumeDlg.exec();
                });
    }
}

void QtVtkModleWindow::CreateCategoryView(SARibbonCategory* page)
{
    // 使用addPannel函数来创建SARibbonPannel，效果和new SARibbonPannel再addPannel一样
    SARibbonPannel* pPannelViewSet = page->addPannel(tr("View"));
    pPannelViewSet->setToolTip(tr("View Set"));
    pPannelViewSet->setObjectName(("pannelViewSet"));

    {
        QAction* pActionViewVertex = CreateAction(tr("Vertex"), "../icon/CreateIcon/My_View_Type_Vertex.png");
        pPannelViewSet->addLargeAction(pActionViewVertex);
        connect(pActionViewVertex, &QAction::triggered, this, [=]()
                {
                    MyVtkViewWindow::GetInstance()->SetViewType(MyVtkViewWindow::E_VIEW_TYPE_VERTEX);
                });
    }

    {
        QAction* pActionViewEdge = CreateAction(tr("Edge"), "../icon/CreateIcon/My_View_Type_Edge.png");
        pPannelViewSet->addLargeAction(pActionViewEdge);
        connect(pActionViewEdge, &QAction::triggered, this, [=]()
                {
                    MyVtkViewWindow::GetInstance()->SetViewType(MyVtkViewWindow::E_VIEW_TYPE_EDGE);
                });
    }

    {
        QAction* pActionViewFace = CreateAction(tr("Face"), "../icon/CreateIcon/My_View_Type_Face.png");
        pPannelViewSet->addLargeAction(pActionViewFace);
        connect(pActionViewFace, &QAction::triggered, this, [=]()
                {
                    MyVtkViewWindow::GetInstance()->SetViewType(MyVtkViewWindow::E_VIEW_TYPE_FACE);
                });
    }

}

void QtVtkModleWindow::CreateQuickAccessBar()
{
    SARibbonQuickAccessBar* quickAccessBar = ribbonBar()->quickAccessBar();

    quickAccessBar->addAction(CreateAction("Save", "../icon/save.svg", "save-quickbar"));
    quickAccessBar->addSeparator();

    QAction* actionUndo = CreateAction("Undo", "../icon/undo.svg");
    actionUndo->setShortcut(QKeySequence("Ctrl+Shift+z"));
    actionUndo->setShortcutContext(Qt::ApplicationShortcut);
    quickAccessBar->addAction(actionUndo);

    QAction* actionRedo = CreateAction("Redo", "../icon/redo.svg");
    actionRedo->setShortcut(QKeySequence("Ctrl+z"));
    actionRedo->setShortcutContext(Qt::ApplicationShortcut);
    quickAccessBar->addAction(actionRedo);
}

void QtVtkModleWindow::CreateRightButtonGroup()
{
    SARibbonBar* ribbon = ribbonBar();
    if(!ribbon)
    {
        return;
    }


    SARibbonButtonGroupWidget* rightBar = ribbon->rightButtonGroup();
    QAction* actionHelp = CreateAction(tr("help"), "../icon/help.svg");
    rightBar->addAction(actionHelp);
}

QAction* QtVtkModleWindow::CreateAction(const QString& text, const QString& iconurl, const QString& objName)
{
    QAction* act = new QAction(this);
    act->setText(text);
    act->setIcon(QIcon(iconurl));
    act->setObjectName(objName);
    return act;
}

QAction* QtVtkModleWindow::CreateAction(const QString& text, const QString& iconurl)
{
    QAction* act = new QAction(this);
    act->setText(text);
    act->setIcon(QIcon(iconurl));
    act->setObjectName(text);
    return act;
}

QWidget* QtVtkModleWindow::CreateCenterViewWidget(QWidget* parent)
{
    auto pViewWidget = MyVtkViewWindow::GetInstance(parent);
    pViewWidget->setMinimumWidth(600);
    pViewWidget->setMinimumHeight(400);
    pViewWidget->setObjectName(("viewWidget"));
    pViewWidget->setStyleSheet("background-color:white;");
    return pViewWidget;
}

ModelTreeWidget* QtVtkModleWindow::CreateModelDataWidget(QWidget* parent)
{
    auto pWidget = new::ModelTreeWidget(parent);
    return pWidget;
}





//// --------------MyProcess.cpp------------------------//
//MyProcess::MyProcess()
//{
//    this->Argc = 0;
//    this->Argv = nullptr;
//}
//
//void MyProcess::SetArgs(int anArgc, char* anArgv[])
//{
//    this->Argc = anArgc;
//    this->Argv = anArgv;
//}
//
//MyProcess* MyProcess::New()
//{
//    return new MyProcess();
//}
//
//void MyProcess::Execute()
//{
//    // multiprocess logic
//    int numProcs = this->Controller->GetNumberOfProcesses();
//    int me = this->Controller->GetLocalProcessId();
//
//    cout << "numProcs=" << numProcs << " me=" << me << endl;
//    cout << "executable=" << this->Argv[0] << endl;
//    cout << "argc=" << this->Argc << endl;
//
//    const int MY_RETURN_VALUE_MESSAGE = 0x11;
//
//    if( me == 0 )
//    {
//        // root node
//        this->ReturnValue = 0;
//        int i = 1;
//        while( i < numProcs )
//        {
//            this->Controller->Send(&this->ReturnValue, 1, i, MY_RETURN_VALUE_MESSAGE);
//            ++i;
//        }
//    }
//    else
//    {
//        // satellites
//        this->Controller->Receive(&this->ReturnValue, 1, 0, MY_RETURN_VALUE_MESSAGE);
//    }
//}